package org.openkoala.koala.popup.javaclass.crud;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.openkoala.koala.console.ConsoleFactory;
import org.openkoala.koala.deploy.curd.generator.CodeGenerator;
import org.openkoala.koala.deploy.curd.module.analysis.CURDCoreAnalysis;
import org.openkoala.koala.deploy.curd.module.analysis.CURDDefaultUIView;
import org.openkoala.koala.deploy.curd.module.core.EntityModel;
import org.openkoala.koala.deploy.curd.module.pojo.NewFile;
import org.openkoala.koala.deploy.curd.module.ui.model.EntityViewUI;
import org.openkoala.koala.exception.KoalaException;
import org.openkoala.koala.pojo.MavenProject;
import org.openkoala.koala.popup.BaseAction;

/**
 * 类    名：CrudAction.java
 *   
 * 功能描述：右键点击java类时执行的action。	
 *  
 * 创建日期：2013-1-21   
 * 
 * 作    者：xmfang(xinmin.fang@gmail.com)
 */
public class CrudAction extends BaseAction {

	private MavenProject project;
	
	private EntityViewUI entityViewUI;
	
	private String errorMessage;
	
	@Override
	public void run(IAction action) {
	    ConsoleFactory.showConsole();
		String javaFilePath = getResource().getLocationURI().getPath();
		analysisJavaClass(javaFilePath);
		
		if (errorMessage != null) {
			MessageDialog.openError(shell, "错误提示", errorMessage);
		    errorMessage = null;
			return;
		}
		
		CrudConfigurationDialog crudConfigurationDialog = new CrudConfigurationDialog(shell, entityViewUI, this);
		crudConfigurationDialog.open();
	}

	private void analysisJavaClass(final String javaFilePath) {
		ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog(shell);
		
		try {
			monitorDialog.run(true, true, new IRunnableWithProgress() {
			    
				@Override
				public void run(IProgressMonitor monitor) throws InvocationTargetException,
						InterruptedException {
				    
					monitor.beginTask("分析Java类...", 2);
					monitor.worked(1);
					CURDCoreAnalysis curdCoreAnalysis = CURDCoreAnalysis.getInstance();
					EntityModel entityModel = null;
					try {
						entityModel = curdCoreAnalysis.analysis(javaFilePath);
					} catch (KoalaException exception) {
						errorMessage = exception.getMessage();
						return;
					}
					
					entityViewUI = CURDDefaultUIView.getDefaultEntityViewUI(entityModel);
					project = curdCoreAnalysis.getProject();
					
					monitor.worked(2);
					
					monitor.done();
				}
			});
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	void showGenerateFilesList(final EntityViewUI entityViewUI) {
		GerenateFilesListDialog gerenateFilesListDialog = new GerenateFilesListDialog(shell, 
				CodeGenerator.getGenerateFiles(entityViewUI, project), project, this);
		gerenateFilesListDialog.open();
	}
	
	void generateCodeByCrudConfig(final List<NewFile> createFiles) {
		final IResource resource = getResource();
		
		ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog(shell);
		try {
			monitorDialog.run(true, true, new IRunnableWithProgress() {
				@Override
				public void run(IProgressMonitor monitor) throws InvocationTargetException,
						InterruptedException {
					monitor.beginTask("生成领域CRUD应用层接口及实现，视图层界面...", 3);
					monitor.worked(1);
					
					monitor.setTaskName("生成领域CRUD应用层接口及实现，视图层界面...");
					try {
						CodeGenerator.generateCode(entityViewUI, project, createFiles);
					} catch (Exception e) {
						e.printStackTrace();
					}
					monitor.worked(2);
					
					monitor.setTaskName("刷新项目...");
					try {
						resource.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, null);
					} catch (CoreException e) {
						e.printStackTrace();
					}
					monitor.worked(3);
					monitor.done();
				}
			});
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
